/**
 * 排班表核心类 - 整合所有管理器，提供统一API
 * 这是插件与系统交互的主要入口
 */

import type {
  Employee,
  Shift,
  ViewMode,
  DateRange,
  ScheduleConfig,
  ScheduleContext,
  Plugin,
} from '../types'
import { EventBus } from './EventBus'
import { PluginManager } from './PluginManager'
import { StateManager } from './StateManager'

export class ScheduleCore {
  private eventBus: EventBus
  private pluginManager: PluginManager
  private stateManager: StateManager
  private config: ScheduleConfig

  constructor(config: ScheduleConfig = {}) {
    this.config = config
    this.eventBus = new EventBus()
    this.stateManager = new StateManager(this.eventBus, config)
    this.pluginManager = new PluginManager()

    // 设置插件管理器的上下文
    this.pluginManager.setContext(this.createContext())
  }

  /**
   * 创建上下文对象供插件使用
   */
  private createContext(): ScheduleContext {
    return {
      // 员工相关
      getEmployees: () => this.stateManager.getEmployees(),
      addEmployee: (employee: Employee) => this.stateManager.addEmployee(employee),
      updateEmployee: (id, employee) => this.stateManager.updateEmployee(id, employee),
      removeEmployee: id => this.stateManager.removeEmployee(id),

      // 班次相关
      getShifts: () => this.stateManager.getShifts(),
      getShiftsByDateRange: (start, end) => this.stateManager.getShiftsByDateRange(start, end),
      getShiftsByEmployee: employeeId => this.stateManager.getShiftsByEmployee(employeeId),
      addShift: (shift: Shift) => this.stateManager.addShift(shift),
      updateShift: (id, shift) => this.stateManager.updateShift(id, shift),
      removeShift: id => this.stateManager.removeShift(id),

      // 视图相关
      getViewMode: () => this.stateManager.getViewMode(),
      setViewMode: (mode: ViewMode) => this.stateManager.setViewMode(mode),

      // 日期范围相关
      getDateRange: () => this.stateManager.getDateRange(),
      setDateRange: (range: DateRange) => this.stateManager.setDateRange(range),

      // 事件总线
      emit: (event, data) => this.eventBus.emit(event, data),
      on: (event, handler) => this.eventBus.on(event, handler),
      off: (event, handler) => this.eventBus.off(event, handler),

      // 插件相关
      getPlugin: id => this.pluginManager.getPlugin(id),
      getPlugins: () => this.pluginManager.getAllPlugins(),
      callPluginMethod: (pluginId, method, ...args) =>
        this.pluginManager.callPluginMethod(pluginId, method, ...args),

      // 配置相关
      getConfig: () => this.stateManager.getConfig(),
      updateConfig: config => this.stateManager.updateConfig(config),
    }
  }

  /**
   * 获取上下文
   */
  getContext(): ScheduleContext {
    return this.createContext()
  }

  /**
   * 安装插件
   */
  async installPlugin(plugin: Plugin): Promise<void> {
    await this.pluginManager.install(plugin)
  }

  /**
   * 批量安装插件
   */
  async installPlugins(plugins: Plugin[]): Promise<void> {
    for (const plugin of plugins) {
      await this.installPlugin(plugin)
    }
  }

  /**
   * 卸载插件
   */
  async uninstallPlugin(pluginId: string): Promise<void> {
    await this.pluginManager.uninstall(pluginId)
  }

  /**
   * 启用插件
   */
  async enablePlugin(pluginId: string): Promise<void> {
    await this.pluginManager.enable(pluginId)
  }

  /**
   * 禁用插件
   */
  async disablePlugin(pluginId: string): Promise<void> {
    await this.pluginManager.disable(pluginId)
  }

  /**
   * 获取所有插件
   */
  getPlugins(): Plugin[] {
    return this.pluginManager.getAllPlugins()
  }

  /**
   * 获取已启用的插件
   */
  getEnabledPlugins(): Plugin[] {
    return this.pluginManager.getEnabledPlugins()
  }

  /**
   * 获取事件总线
   */
  getEventBus(): EventBus {
    return this.eventBus
  }

  /**
   * 获取状态管理器
   */
  getStateManager(): StateManager {
    return this.stateManager
  }

  /**
   * 获取插件管理器
   */
  getPluginManager(): PluginManager {
    return this.pluginManager
  }

  /**
   * 获取配置
   */
  getConfig(): ScheduleConfig {
    return this.stateManager.getConfig()
  }

  /**
   * 更新配置
   */
  updateConfig(config: Partial<ScheduleConfig>): void {
    this.stateManager.updateConfig(config)
  }

  /**
   * 销毁实例，清理资源
   */
  destroy(): void {
    this.eventBus.clear()
    this.pluginManager.clear()
    this.stateManager.clear()
  }
}
